package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityData;
import com.sailfish.springbootdemo.pojo.db2.PcbConfig;
import com.sailfish.springbootdemo.pojo.db2.PcbCost;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbCostDao {
    @Results(id = "pcbCostResultMap", value = {
            @Result(column = "pcb_cost_id", property = "pcbCostId", jdbcType = JdbcType.INTEGER),
            @Result(column = "project_id", property = "projectId", jdbcType = JdbcType.INTEGER),
            @Result(column = "board_name", property = "boardName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "product_code0301", property = "productCode0301", jdbcType = JdbcType.VARCHAR),
            @Result(column = "layer", property = "layer", jdbcType = JdbcType.VARCHAR),
            @Result(column = "length", property = "length", jdbcType = JdbcType.VARCHAR),
            @Result(column = "width", property = "width", jdbcType = JdbcType.VARCHAR),
            @Result(column = "thickness", property = "thickness", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_id", property = "factoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "cost", property = "cost", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select pcb_cost_id, project_id, board_name, version, product_code0301, layer, length, width,thickness,factory_id, cost, remark, create_time, create_user, update_time, update_user, status, sort " +
            " from " + DB2TableName.PCB_COST +
            " where pcb_cost_id = #{id}" +
            "</script>")
    PcbCost getById(@Param("id") Integer id);

    /*
     *查询单个
     */
    @Select("<script> " +
            "select distinct board_name, version  " +
            " from " + DB2TableName.PCB_COST +
            "</script>")
    @ResultMap(value = "pcbCostResultMap")
    List<PcbCost> getProductList();

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select pcb_cost_id, project_id, board_name, version, product_code0301, layer, length, width,thickness, factory_id, cost, remark, create_time, create_user, update_time, update_user, status, sort " +
            " from " + DB2TableName.PCB_COST +
            "<where>" +
            "<if test='projectId != null'>" +
            " and project_id = #{projectId}" +
            "</if>" +
            "<if test='boardName != null'>" +
            " and board_name = #{boardName}" +
            "</if>" +
            "<if test='version != null'>" +
            " and version = #{version}" +
            "</if>" +
            "<if test='productCode0301 != null'>" +
            " and product_code0301 = #{productCode0301}" +
            "</if>" +
            "<if test='layer != null'>" +
            " and layer = #{layer}" +
            "</if>" +
            "<if test='factoryId != null'>" +
            " and factoryId = #{factoryId}" +
            "</if>" +
            "<if test='cost != null'>" +
            " and cost = #{cost}" +
            "</if>" +
            "<if test='remark != null'>" +
            " and remark = #{remark}" +
            "</if>" +
            "<if test='createUser != null'>" +
            " and createUser = #{createUser}" +
            "</if>" +
            "<if test='status != null'>" +
            " and status = #{status}" +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap(value = "pcbCostResultMap")
    List<PcbCost> getByLimit(PcbCost param);

    @Insert("insert into " + DB2TableName.PCB_COST + "(project_id, board_name, version, product_code0301, layer, length, width, thickness, factory_id, cost, remark, create_time, create_user, update_time, update_user, status, sort) " +
            "values (#{projectId}, #{boardName}, #{version}, #{productCode0301},#{layer},#{length},#{width},#{thickness},#{factoryId}, #{cost}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbCostId")
    Integer insert(PcbCost pcbCost);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_COST + " where pcb_cost_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    /*
     *通过单板信息删除
     */
    @Delete("<script>" +
            "delete from " + DB2TableName.PCB_COST +
            "<where>" +
            "<if test = 'boardName != null and boardName != \"\"'>" +
            " and board_name = #{boardName} " +
            "</if>" +
            "<if test = 'version != null and version != \"\"'>" +
            " and version = #{version}" +
            "</if>" +
            "</where>" +
            "</script>")
    Integer deleteByProduct(@Param("boardName") String boardName, @Param("version") String version);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_COST +
            "<set>" +
            "<if test='projectId != null'>" +
            " project_id = #{projectId}," +
            "</if>" +
            "<if test='boardName != null'>" +
            " board_name = #{boardName}," +
            "</if>" +
            "<if test='version != null'>" +
            " version = #{version}," +
            "</if>" +
            "<if test='factoryId != null'>" +
            "factory_id = #{factoryId}," +
            "</if>" +
            "<if test='cost != null'>" +
            "cost = #{cost}," +
            "</if>" +
            "<if test='remark != null'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='createUser != null'>" +
            "createUser = #{createUser}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='updateUser != null'>" +
            "update_user = #{updateUser}," +
            "</if>" +
            "<if test='sort != null'>" +
            "sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "status = #{status}," +
            "</if>" +
            "</set>" +
            "where pcb_cost_id = #{pcbCostId}" +
            "</script>")
    Integer update(PcbCost param);
}
